# 通用模块Makefile
# 管理所有通用工具模块的编译
# 采用头文件引用方式，不生成静态库

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
INCLUDES = -I./utils/include -I./version/include -I./json/include -I./config/include -I./network/include -I./logs/include -I../third/cJSON

# 构建目录设置
BUILD_DIR = ../build/common

# 通用工具模块源文件
UTILS_SOURCES = utils/src/buffer_utils.c utils/src/memory_utils.c utils/src/string_utils.c

# 版本管理模块源文件
VERSION_SOURCES = version/src/version.c

# JSON工具模块源文件
JSON_SOURCES = json/src/json_utils.c json/src/param_parser.c

# 配置工具模块源文件
CONFIG_SOURCES = config/src/cfg_utils.c

# 网络工具模块源文件
NETWORK_SOURCES = network/src/network_utils.c

# 日志模块源文件
LOGS_SOURCES = logs/src/logger.c

# 所有源文件
ALL_SOURCES = $(UTILS_SOURCES) $(VERSION_SOURCES) $(JSON_SOURCES) $(CONFIG_SOURCES) $(NETWORK_SOURCES) $(LOGS_SOURCES)

# 默认目标 - 只检查编译，不生成库
all: check_compile

# 检查编译 - 验证所有源文件都能正确编译
check_compile:
	@echo "检查通用模块编译..."
	@for src in $(ALL_SOURCES); do \
		echo "检查: $$src"; \
		obj_path=$(BUILD_DIR)/$$(dirname $$src)/$$(basename $$src .c).o; \
		obj_dir=$$(dirname $$obj_path); \
		mkdir -p $$obj_dir; \
		$(CC) $(CFLAGS) $(INCLUDES) -c $$src -o $$obj_path; \
	done
	@echo "通用模块编译检查完成"

# 清理
clean:
	@echo "清理通用模块临时文件..."
	@rm -rf $(BUILD_DIR)
	@echo "通用模块清理完成"

# 安装头文件
install-headers:
	@echo "安装通用模块头文件..."
	@mkdir -p $(DESTDIR)/usr/local/include/dlsystem
	@cp -r utils/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@cp -r version/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@cp -r json/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@cp -r config/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@cp -r network/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@cp -r logs/include/* $(DESTDIR)/usr/local/include/dlsystem/
	@echo "通用模块头文件安装完成"

# 安装
install: install-headers

# 帮助
help:
	@echo "通用模块构建系统"
	@echo ""
	@echo "可用目标:"
	@echo "  all          - 检查通用模块编译 (默认)"
	@echo "  clean        - 清理临时文件"
	@echo "  install      - 安装头文件"
	@echo "  help         - 显示此帮助信息"
	@echo ""
	@echo "注意: 通用模块采用头文件引用方式，不生成静态库"

.PHONY: all clean install install-headers help check_compile
